//---------------------------------------------------------------------------
// Name:	elog.cpp
// Desc:	Logger source for SymbianOS
// Author:	D.C
// Date:	18:04 2006-10-17
//---------------------------------------------------------------------------
#include "elog.h"   																 

#ifdef _LOG
 
_LIT8(t, ","); 
#ifdef _TIMESTAMP 
_LIT(ss, "\r\n%-B%:0%J%:1%T%:2%S%.%*C4%:3%+B"); 
#else
_LIT(ss, "\r\n");
#endif

#ifdef __WINS__
_LIT(KTheLogFile, "c:\\log.txt");
#else
_LIT(KTheLogFile, "e:\\log.txt");
#endif

const TInt KFileOpenWritable = EFileWrite | EFileShareAny | EFileStream;			   

class TLoggerOverflow : public TDes8Overflow
{
public:
    void Overflow(TDes8& /*aDes*/)
    {
    }
};

class TLoggerOverflow16 : public TDes16Overflow
{
public:
    void Overflow(TDes16& /*aDes*/)
    {
    }
};

void CEpocLogger::Clear()
{
    RFs fs;
    User::LeaveIfError(fs.Connect()); 
    TEntry gg;
    TInt ret = fs.Entry(KTheLogFile, gg);

    if (KErrNone == ret)
    {
        fs.Delete(KTheLogFile);
    }
    fs.Close();
}

void CEpocLogger::DoLog(TRefByValue<const TDesC8> aFmt, ...)
{
    TBuf8<128> sss; 
#ifdef _TIMESTAMP    
    TTime time; 
    time.HomeTime();
    TBuf<128> s; 
    time.FormatL( s, ss ); 
    sss.Copy(s); 
    sss.Append(t); 
#else
    sss.Copy( ss );
#endif

    VA_LIST list;
    VA_START(list, aFmt);

    TLoggerOverflow logOverflow;
    TBuf8<1024> logBuf;
    logBuf.AppendFormatList(aFmt, list, &logOverflow);

    RFs theLogFs;
    theLogFs.Connect();

    RFile theLogFile;
    TInt ret = theLogFile.Open(theLogFs, KTheLogFile, KFileOpenWritable);
    if (ret == KErrNotFound)
        ret = theLogFile.Replace(theLogFs, KTheLogFile, KFileOpenWritable);
    if (ret != KErrNone)
        return;

    TInt pos = 0;
    ret = theLogFile.Seek(ESeekEnd, pos);
    if (ret == KErrNone)
    {
        ret = theLogFile.Write(sss);
    }
    if (ret == KErrNone)
    {
        theLogFile.Write(logBuf);
    }


    theLogFile.Flush();
    theLogFile.Close();
    theLogFs.Close();
}

void CEpocLogger::DoLog(TRefByValue<const TDesC16> aFmt, ...)
{
    TBuf8<128> sss; 
#ifdef _TIMESTAMP    
    TTime time; 
    time.HomeTime();
    TBuf<128> s; 
    time.FormatL( s, ss ); 
    sss.Copy(s); 
    sss.Append(t); 
#else
    sss.Copy( ss );
#endif

    VA_LIST list;
    VA_START(list, aFmt);

    TLoggerOverflow16 logOverflow;
    TBuf16<1024> logBuf;
    logBuf.AppendFormatList(aFmt, list, &logOverflow);

    RFs theLogFs;
    theLogFs.Connect();

    RFile theLogFile;
    TInt ret = theLogFile.Open(theLogFs, KTheLogFile, KFileOpenWritable);
    if (ret == KErrNotFound)
        ret = theLogFile.Replace(theLogFs, KTheLogFile, KFileOpenWritable);
    if (ret != KErrNone)
        return;

    TInt pos = 0;
    ret = theLogFile.Seek(ESeekEnd, pos);
    if (ret == KErrNone)
    {
        ret = theLogFile.Write(sss);
    }
    if (ret == KErrNone)
    {
        HBufC8* p = HBufC8::New( logBuf.Length() + 1);
        TPtr8 pp( p->Des());
        pp.Copy( logBuf );
        theLogFile.Write(*p);
        SAFE_DELETE( p );
    }


    theLogFile.Flush();
    theLogFile.Close();
    theLogFs.Close();
}

void CEpocLogger::DoLog(const TDesC8& aText)
{
    TBuf8<128> sss; 
#ifdef _TIMESTAMP    
    TTime time; 
    time.HomeTime();
    TBuf<128> s; 
    time.FormatL( s, ss ); 
    sss.Copy(s); 
    sss.Append(t); 
#else
    sss.Copy( ss );
#endif

    RFs theLogFs;
    theLogFs.Connect();

    RFile theLogFile;
    TInt ret = theLogFile.Open(theLogFs, KTheLogFile, KFileOpenWritable);
    if (ret == KErrNotFound)
        ret = theLogFile.Replace(theLogFs, KTheLogFile, KFileOpenWritable);
    if (ret != KErrNone)
        return;

    TInt pos = 0;
    ret = theLogFile.Seek(ESeekEnd, pos);
    if (ret == KErrNone)
    {
        ret = theLogFile.Write(sss);
    }
    if (ret == KErrNone)
    {
        theLogFile.Write(aText);
    }
    theLogFile.Flush();
    theLogFile.Close();
    theLogFs.Close();
}

void CEpocLogger::DoLog(const TDesC16& aText)
{
     
    TBuf8<128> sss; 
#ifdef _TIMESTAMP    
    TTime time; 
    time.HomeTime();
    TBuf<128> s; 
    time.FormatL( s, ss ); 
    sss.Copy(s); 
    sss.Append(t); 
#else
    sss.Copy( ss );
#endif

    RFs theLogFs;
    theLogFs.Connect();

    RFile theLogFile;
    TInt ret = theLogFile.Open(theLogFs, KTheLogFile, KFileOpenWritable);
    if (ret == KErrNotFound)
        ret = theLogFile.Replace(theLogFs, KTheLogFile, KFileOpenWritable);
    if (ret != KErrNone)
        return;

    TInt pos = 0;
    ret = theLogFile.Seek(ESeekEnd, pos);
    if (ret == KErrNone)
    {
        ret = theLogFile.Write(sss);
    }
    if (ret == KErrNone)
    {
        HBufC8* p = HBufC8::New( aText.Length() + 1);
        TPtr8 pp( p->Des());
        pp.Copy( aText );
        theLogFile.Write( *p );
        SAFE_DELETE( p );
    }


    theLogFile.Flush();
    theLogFile.Close();
    theLogFs.Close();
}


#endif // _LOG  																	   
